/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bstek.ureport.build.paging.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.dto.contract.ContractBuildingDTO;
import org.springblade.modules.repair.entity.BuildingContractPlus;
import org.springblade.modules.repair.service.IBuildingContractPlusService;
import org.springblade.modules.repair.vo.BuildingContractPlusVO;
import org.springblade.modules.repair.vo.contract.*;
import org.springblade.modules.repair.wrapper.BuildingContractPlusWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目合同信息 控制器
 *
 * @author ysq
 * @since 2022-10-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/buildingcontractplus")
@Api(value = "项目合同信息", tags = "项目合同信息接口")
public class BuildingContractPlusController extends BladeController {

	private final IBuildingContractPlusService buildingContractPlusService;


	@GetMapping("getContractStatusList")
	@ApiOperation("获取合同的统计信息")
	R<ContractStatusListVo> getContractStatusList(Date beginTime, Date endTime) {
		return R.data(buildingContractPlusService.getContractStatusList(beginTime, endTime));
	}


	/**
	 * 分页
	 *
	 * @param buildingContractPlus 查询条件
	 * @param query                分页
	 * @return 数据集
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入buildingContractPlus")
	public R<IPage<BuildingContractPlusVO>> list(BuildingContractPlus buildingContractPlus, Query query) {
		buildingContractPlus.setDisDeleted(0);
		if (buildingContractPlus.getBeginTime() == null) {
			QueryWrapper<BuildingContractPlus> queryWrapper = Condition.getQueryWrapper(buildingContractPlus);
			queryWrapper.orderByDesc("create_time");
			IPage<BuildingContractPlus> pages = buildingContractPlusService.page(Condition.getPage(query), queryWrapper);
			return R.data(BuildingContractPlusWrapper.build().pageVO(pages));
		} else {
			IPage<BuildingContractPlus> pages = buildingContractPlusService.page(Condition.getPage(query), Wrappers.<BuildingContractPlus>lambdaQuery().orderByDesc(BuildingContractPlus::getCreateTime).eq(BuildingContractPlus::getContractSource, buildingContractPlus.getContractSource()).between(BuildingContractPlus::getCreateTime, buildingContractPlus.getBeginTime(), buildingContractPlus.getEndTime()));
			return R.data(BuildingContractPlusWrapper.build().pageVO(pages));
		}
	}

	/**
	 * 分页 项目合同信息
	 */
	@GetMapping("/getTotalDesc")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取统计文案", notes = "传入buildingContractPlus")
	public R<ContractTotalVo> getTotalDest(BuildingContractPlus buildingContractPlus) {
		List<BuildingContractPlus> list = buildingContractPlusService.list(buildingContractPlus);
		List<String> buildingIds = new ArrayList<>(16);
		List<String> elevatorIds = new ArrayList<>(16);
		for (BuildingContractPlus buildingContract : list) {
			List<String> buildingId = Arrays.asList(buildingContract.getBuildingIds().split(","));
			buildingIds.addAll(buildingId);
			List<String> elevatorId = Arrays.asList(buildingContract.getElevatorIds().split(","));
			elevatorIds.addAll(elevatorId);
		}
		Integer elevatorCounts = elevatorIds.stream().distinct().collect(Collectors.toList()).size();
		Integer buildingCounts = buildingIds.stream().distinct().collect(Collectors.toList()).size();
		ContractTotalVo contractTotalVo = new ContractTotalVo();
		contractTotalVo.setContractCounts(list.size());
		contractTotalVo.setElevatorCounts(elevatorCounts);
		contractTotalVo.setBuildingCounts(buildingCounts);
		return R.data(contractTotalVo);
	}

	/**
	 * 获取合同统计信息
	 */
	@GetMapping("/getContractStatisticInfo")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取合同在保信息")
	public R<ContractStatisticVo> getContractStatisticInfo(){
		return R.data(buildingContractPlusService.getContractStatisticInfo());
	}

	/**
	 * 获取合同统计信息
	 */
	@GetMapping("/getBuildingCount")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取在保信息的项目数")
	public R<ContractStatusVo> getBuildingCount(@RequestParam(defaultValue = "0") Integer type){
		return R.data(buildingContractPlusService.getBuildingCount(type));
	}


	/**
	 * 获取合同统计信息
	 */
	@PostMapping("/getElevatorList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取在保信息的电梯列表")
	public R<IPage<ContractBuildingVo>> getElevatorListOfInTime(@RequestBody ContractBuildingDTO contractBuildingDTO){
		return R.data(buildingContractPlusService.getElevatorList(contractBuildingDTO));
	}

}
